import sys
import common
#if len(sys.argv)<2:   
#     print("usage:")  
#     exit(0)          
#inf= sys.argv[1]      
inf='dex-vaule-fmt.txt'
                       
myfile = open(inf, "r")
l=[]
ll=[]

for line in (myfile):
	s=line.strip(' \t\n')
	if len(s)<1:
		continue
		
	if s.startswith('#'):
		continue
		
	s1=s.split('\t')
	ll.append(s1)

print('''
#include <stdio.h>
#include "encoded-value.h"
#include "dex-structs.h"

int CDexEncodedValue::Size()
{
  int s=sizeof(ubyte);
	switch(value_type)
	{''')	
for x in ll:
	if x[3].endswith('[size]'):
		continue
		
	print('\tcase %s:'%(x[0]))
	if x[3].endswith('[1]'):
		print('\t\ts++;')
	elif x[3]!='(none)':
		s=common.get_cls_name(x[3])
		print('''
		{
		  %s *p=(%s *)(this+1);
		  s+=p->Size();
		 }	
		'''%(s,s))
	print('\tbreak;')
	
for x in ll:
	if x[3].endswith('[size]'):
		print('\tcase %s:'%(x[0]))
			
		


print('\t\ts+=value_arg;')
print('\tbreak;')	
print('''
  }
  return s;
}
	''')	
print('''	
void CDexEncodedValue::print()
{
  int j;


  
	switch(value_type)
	{''')	
for x in ll:

	print('\tcase %s:'%(x[0]))
	if x[3].endswith('[1]'):
		print('''
        printf("%02x",((char *)(this+1))[0]);
		''')
	elif x[3].endswith('[size]'):
		l=str((int(x[2][-2:-1])+1)<<1)
		t=x[0][6:].lower()
		if t=='float':
			f='%f'
		elif t=='double':
			f='%le'
		else:
			f='%0'+l+'x'
			
		if t=='long':
			t='__int64'
		elif t=='char':
			t='short'
		elif t=='method' or t=='string' or t=='type' or t=='field' or t=='enum':
			t='int'
 
		print('''
		{
         union
         {
           %s v;
           char buf[1];
         }u;		
        char *t=(char*)(this+1);
        u.v=0;
        for(j=0;j<value_arg;j++) u.buf[j]=t[j];        
        
        printf("%s",u.v);        
     }
   	'''%(t,f))
	elif x[3].startswith('e'):
		s=common.get_cls_name(x[3])
		print('''
		{
		  %s *p=(%s *)(this+1);
		  p->print();
		 }	
		'''%(s,s))
	elif x[2].startswith('bool'):
		print('\t\tprintf("%s",value_arg==1?"true":"false");')
	else:
		print('\t\tprintf("null");')
		
	print('\tbreak;')
	
	
print('''
  }

}
	''')		
	
print('''	
int CDexEncodedValue::sprint(char * buffer)
{
  int j;
	int len=0;

  
	switch(value_type)
	{''')	
for x in ll:

	print('\tcase %s:'%(x[0]))
	if x[3].endswith('[1]'):
		print('''
        len+=printf(buffer+len,"%02x",((char *)(this+1))[0]);
		''')
	elif x[3].endswith('[size]'):
		l=str((int(x[2][-2:-1])+1)<<1)
		t=x[0][6:].lower()
		if t=='float':
			f='%f'
		elif t=='double':
			f='%le'
		else:
			f='%0'+l+'x'
			
		if t=='long':
			t='__int64'
		elif t=='char':
			t='short'
		elif t=='method' or t=='string' or t=='type' or t=='field' or t=='enum':
			t='int'
 
		print('''
		{
         union
         {
           %s v;
           char buf[1];
         }u;		
        char *t=(char*)(this+1);
        u.v=0;
        for(j=0;j<value_arg;j++) u.buf[j]=t[j];                
        	len+=printf(buffer+len,"%s",u.v);        
     }
   	'''%(t,f))
	elif x[3].startswith('e'):
		s=common.get_cls_name(x[3])
		print('''
		{
		  %s *p=(%s *)(this+1);
		  len+=p->sprint(buffer+len);
		 }	
		'''%(s,s))
	elif x[2].startswith('bool'):
		print('\t\tlen+=printf(buffer+len,"%s",value_arg==1?"true":"false");')
	else:
		print('\t\tlen+=printf(buffer+len,"null");')
		
	print('\tbreak;')
	
	
print('''
  }
  return len;

}
	''')			